
package control;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UsuarioDao;  
import model.Usuario;  
/**
 * @author Renata
 */
@WebServlet("/EnviarDados") 
public class EnviarDados extends HttpServlet {

   private static final long serialVersionUID = 1L;  
         
    public EnviarDados() {  
        super();  
    }  
  
   protected void doGet(HttpServletRequest request, HttpServletResponse response) 
           throws ServletException, IOException {  
   }  
  
  protected void doPost(HttpServletRequest request, HttpServletResponse response) 
          throws ServletException, IOException {  
      try{  
           
         String cmd = request.getParameter("cmd");  
         Usuario user;  
         UsuarioDao userDao;
           
         if(cmd.equalsIgnoreCase("cadastrar")){  
            user = new Usuario(request.getParameter("nome"),   
                   request.getParameter("senha"),
                    request.getParameter("login"),
                    Integer.parseInt(request.getParameter("tipo"))); 
                 
              
            userDao = new UsuarioDao();  
            userDao.create(user);
            userDao.atualizaId(user);
            userDao.inserirNoRanking(user);
            userDao.inserirNasEstatisticas(user);
              
            request.setAttribute("msg", "Cadastro efetuado com sucesso!");  
            request.getRequestDispatcher("index.jsp").forward(request, response);  
         }  
           
      }catch (Exception e) {  
         request.setAttribute("erro", "Erro: " + e.getMessage());
         
         if(e.getMessage().equalsIgnoreCase("Duplicate entry" +"login"+ "for key 'login'")){
             request.getRequestDispatcher("sqlErro.jsp").forward(request, response);
         }
         

         request.getRequestDispatcher("erro.jsp").forward(request, response);  
      }  
   }  
}



